package com.example.exercise.sys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.sys.vo.SysRoleVO;
import com.example.exercise.sys.dao.entity.SysRoleEntity;
import com.example.exercise.sys.dao.SysRoleDAO;

/**
* CRM角色表
*/
@Service
public class SysRoleService {

    @Autowired
    private SysRoleDAO sysRoleDAO;

    public List<SysRoleVO> findByWhere(Page<?> page, SysRoleVO sysRoleVO){
        List<SysRoleVO> list = new ArrayList<>();
        List<SysRoleEntity> list0 = sysRoleDAO.findByWhere(page, sysRoleVO);
        for (SysRoleEntity sysRoleEntity : list0) {
            list.add(toSysRoleVO(sysRoleEntity));
        }
        return list;
    }

    public SysRoleVO findByKey(SysRoleVO sysRoleVO){
        SysRoleEntity sysRoleEntity = sysRoleDAO.findByKey(sysRoleVO);
        return toSysRoleVO(sysRoleEntity);
    }

    public int insert(SysRoleVO sysRoleVO){
        return sysRoleDAO.insert(toSysRoleEntity(sysRoleVO));
    }

    public int updateByKey(SysRoleVO sysRoleVO){
        return sysRoleDAO.updateByKey(toSysRoleEntity(sysRoleVO));
    }

    public int deleteByKey(SysRoleVO sysRoleVO){
        return sysRoleDAO.deleteByKey(sysRoleVO);
    }

    private SysRoleVO toSysRoleVO(SysRoleEntity sysRoleEntity) {
        SysRoleVO sysRoleVO = new SysRoleVO();
                sysRoleVO.setId(String.valueOf(sysRoleEntity.getId()));
        sysRoleVO.setRolename(sysRoleEntity.getRolename());

        return sysRoleVO;
    }

    private SysRoleEntity toSysRoleEntity(SysRoleVO sysRoleVO) {
        SysRoleEntity sysRoleEntity = new SysRoleEntity();
                String id = sysRoleVO.getId();
        if(id != null) {
            sysRoleEntity.setId(Integer.parseInt(sysRoleVO.getId()));
        }
        sysRoleEntity.setRolename(sysRoleVO.getRolename());

        return sysRoleEntity;
    }

}